/*
 * GPSPacket.java
 *
 * Created on April 15, 2008, 9:28 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.sunspotworld.mydatawrappers;

import com.sun.spot.peripheral.Spot;
import com.sun.spot.util.IEEEAddress;
import java.io.IOException;
import java.util.Date;
import org.sunspotworld.sensortasks.TimeKeeper;

/**
 * A data-wrapper class for encapsulating methods to transmit, receive and record information about GPS readings
 * @author Douglas McClusky
 */
public final class GPSPacket extends TrackingPacket {
    private double latitude;
    private double longitude;
    private double hdop;
    public static final String HEADER = "Date\tTime\tNode\tLatitude\tLongitude\tHDOP";
    public static final int SIZE = TrackingPacket.SIGN_SIZE + 3*8; // 3 doubles + header
    /*    
    protected GPSPacket(byte[] b) {
        super.readSign(b);
        int temp = super.signSize;
        super.timestamp = super.byteArrayToLong(b, temp);
        temp += 8;
        latitude = super.byteArrayToDouble(b, temp);
        temp += 8;
        longitude = super.byteArrayToDouble(b, temp);
        temp += 8;
        hdop = super.byteArrayToDouble(b, temp);
    } //*/
    
    protected GPSPacket(byte[] b, int index) {
        super.readSign(b, index);
        int temp = super.SIGN_SIZE + index;
        latitude = super.byteArrayToDouble(b, temp);
        temp += 8;
        longitude = super.byteArrayToDouble(b, temp);
        temp += 8;
        hdop = super.byteArrayToDouble(b, temp);
    }
    
    /**
     * Creates a new instance of GPSPacket 
     * @param lat - the latitude of the GPS reading
     * @param lon - the longitude of the GPS reading
     * @param hdop - the HDOP of the GPS reading
     * @param time - the time when the GPS reading was taken
     */
    public GPSPacket(double lat, double lon, double dop) {
        super.type = super.GPS_PACKET;
        super.senderAddress = Spot.getInstance().getRadioPolicyManager().getIEEEAddress();
        latitude = lat;
        longitude = lon;
        super.timestamp = TimeKeeper.getInstance().currentTimeMillis();
        hdop = dop;
    }   
    
    /**
     * A String representation of this GPSPacket for writing records
     * @return the packet's information in comma-delimited format
     */
    public String printEntryLine() {
        Date time = new Date(super.timestamp);
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd '\t' HH:mm:ss.SSS");
        return time + "\t" + new IEEEAddress(super.senderAddress).asDottedHex() + "\t" + latitude + "\t" + longitude + "\t" + hdop;
    }

    /**
     * A method to track the size of this packet for keeping track of Radiogram space use
     * @return the space used by writeToRadiogram in bytes.
     */
    public int getSize() {
        return SIZE;
    }
    
    public byte[] toByteArray() {
        byte[] b = new byte[getSize()];
        int i;
        super.sign(b);
        int temp = super.SIGN_SIZE;
        byte[] tempArray = super.doubleToByteArray(latitude);
        for (i=0; i<tempArray.length; i++) {
            b[temp] = tempArray[i];
            temp++;
        }
        tempArray = super.doubleToByteArray(longitude);
        for (i=0; i<tempArray.length; i++) {
            b[temp] = tempArray[i];
            temp++;
        }
        tempArray = super.doubleToByteArray(hdop);
        for (i=0; i<tempArray.length; i++) {
            b[temp] = tempArray[i];
            temp++;
        }
        //*
        try {
            System.out.println(this.printEntryLine());
            System.out.println(new GPSPacket(b, 0).printEntryLine());
            System.out.println(TrackingPacketFactory.getInstance().constructDataWrapper(b).printEntryLine());
        } catch (IOException ex) {
            ex.printStackTrace();
        } //*/
        return b;
    }
}
